package com.njrsrc.common.parser.json;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 地区Json解析
 * 
 * @author Administrator
 * 
 */
public class AreaParser {
	private List<ProvinceInfo> provinceList;
	private List<CityInfo> cityList;
	private List<ZoneInfo> zoneList;
	private InputStream inputStream;
	private ProvinceInfo provinceInfo;
	private CityInfo cityInfo;
	private ZoneInfo zoneInfo;

	public AreaParser(InputStream inputStream) {
		this.inputStream = inputStream;
		provinceList = new ArrayList<ProvinceInfo>();
		cityList = new ArrayList<CityInfo>();
		zoneList = new ArrayList<ZoneInfo>();
	}

	public List<ProvinceInfo> getProvinceList() {
		byte[] data = null;
		try {
			data = readStream(inputStream);
			String json = new String(data);
			System.out.println(json);
			JSONArray provinceArray = new JSONArray(json);
			for (int i = 0; i < provinceArray.length(); i++) {
				provinceInfo = new ProvinceInfo();
				JSONObject provinceObject = provinceArray.optJSONObject(i);
				JSONArray cityArray = (JSONArray) provinceObject.opt("items");
				if (cityArray == null) {
					// 没有叶子节点
					provinceInfo.setCode((String) provinceObject.opt("code"));
					provinceInfo.setName((String) provinceObject.opt("name"));
					provinceInfo.setCityList(null);
				} else {
					// 说明有市级列表
					cityList = new ArrayList<CityInfo>();
					for (int j = 0; j < cityArray.length(); j++) {
						cityInfo = new CityInfo();
						JSONObject cityObject = cityArray.optJSONObject(j);
						JSONArray zoneArray = (JSONArray) cityObject.opt("items");
						if (zoneArray == null) {
							// 没有叶子节点
							cityInfo.setCode((String) cityObject.opt("code"));
							cityInfo.setName((String) cityObject.opt("name"));
							provinceInfo.setCode((String) provinceObject.opt("code"));
							provinceInfo.setName((String) provinceObject.opt("name"));
							cityInfo.setZoneList(null);
						} else {
							zoneList = new ArrayList<ZoneInfo>();
							// 说明有区级列表
							for (int k = 0; k < zoneArray.length(); k++) {
								zoneInfo = new ZoneInfo();
								cityInfo = new CityInfo();
								zoneInfo.setCode((String) ((JSONObject) zoneArray.opt(k)).opt("code"));
								zoneInfo.setName((String) ((JSONObject) zoneArray.opt(k)).opt("name"));
								cityInfo.setCode((String) cityObject.opt("code"));
								cityInfo.setName((String) cityObject.opt("name"));
								provinceInfo.setCode((String) provinceObject.opt("code"));
								provinceInfo.setName((String) provinceObject.opt("name"));
								zoneList.add(zoneInfo);
								zoneInfo = null;
							}
							cityInfo.setZoneList(zoneList);
							zoneList = null;
						}
						cityList.add(cityInfo);
						cityInfo = null;
					}
					provinceInfo.setCityList(cityList);
					cityList = null;
				}
				provinceList.add(provinceInfo);
				provinceInfo = null;
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return provinceList;
	}

	/**
	 * 把输入流转换成字符数组
	 * 
	 * @param inputStream
	 *            输入流
	 * @return 字符数组
	 * @throws IOException
	 * @throws Exception
	 */
	public static byte[] readStream(InputStream inputStream) throws IOException {
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = inputStream.read(buffer)) != -1) {
			bout.write(buffer, 0, len);
		}
		bout.close();
		inputStream.close();
		return bout.toByteArray();
	}
}